// pages/quadradic1/quadradic1.js

var utils = require('../../utils/util.js')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    a: '',
    b: '',
    c: '',
    equation: 'x²',
    isTopRetMinusX: false,
    isTopRetMinusY: false,
    showRet: false,
    squareNum: 0,
    topPoint: {
      x: {
        fenzi: 0,
        fenmu: 0
      },
      y: {
        fenzi: 0,
        fenmu: 0
      }
    },
    crossPointY: {
      fenzi: 0,
      fenmu: 0
    }
  },

  //a值输入框值改变事件
  inputA: function(e) {
    let val = e.detail.value
    let isNum = utils.judgeNum(val)
    if (val === '' || isNum) {
      this.setData({
        a: e.detail.value
      })
      this.buildEquation()
    } else {
      this.setData({
        a: this.data.a
      })
    }
  },

  //b值输入框值改变事件
  inputB: function(e) {
    let val = e.detail.value
    let isNum = utils.judgeNum(val)
    if (val === '' || isNum) {
      this.setData({
        b: e.detail.value
      })
      this.buildEquation()
    } else {
      this.setData({
        b: this.data.b
      })
    }
  },

  //c值输入框值改变事件
  inputC: function(e) {
    let val = e.detail.value
    let isNum = utils.judgeNum(val)
    if (val === '' || isNum) {
      this.setData({
        c: e.detail.value
      })
      this.buildEquation()
    } else {
      this.setData({
        c: this.data.c
      })
    }
  },

  //构建二次函数解析式
  buildEquation: function() {
    let equation = ''
    if (this.data.a === '1') {
      equation = 'x²'
    } else if (this.data.a === '-1') {
      equation = '-x²'
    } else {
      equation = this.data.a + 'x²'
    }

    if (this.data.b === '1') {
      equation += 'x'
    } else if (this.data.b === '-1' || this.data.b === '-') {
      equation += '-x'
    } else if (this.data.b !== '') {
      equation += '+' + this.data.b + 'x'
    }
    if (parseFloat(this.data.c) > 0) {
      equation += '+' + this.data.c
    } else(
      equation += this.data.c
    )
    this.setData({
      equation: equation
    })
  },

  // 计算按钮点击事件
  count: function() {
    let a, b, c
    this.data.a === '' ? a = 1 : a = parseFloat(this.data.a)
    this.data.b === '' ? b = 0 : b = parseFloat(this.data.b)
    this.data.c === '' ? c = 0 : c = parseFloat(this.data.c)
    // 如果a值为空或为0提示a值错误
    if (this.data.a === '0' || parseFloat(this.data.a) === 0) {
      wx.showModal({
        title: '错误',
        content: '二次函数二次项系数a不能为0',
        showCancel: false
      })
      return
    }
    console.log(utils.judgeNum(a))
    console.log(utils.judgeNum(b))
    if (!utils.judgeNum(a) || !utils.judgeNum(b) || !utils.judgeNum(c)) {
      wx.showToast({
        title: '数值输入错误',
        icon: 'none'
      })
      return
    }

    let fenmu = -2 * a
    console.log('分母是', fenmu)
    let topPointX = utils.getShang(b, fenmu)
    console.log('顶点横坐标是', topPointX)

    let fenzi = 4 * a * c - b * b;
    fenmu = 4 * a
    console.log('y交点的分子是', fenzi)
    console.log('y的分母是', fenmu)
    let topPointY = utils.getShang(fenzi, fenmu)
    console.log('y的顶点坐标是', topPointY)

    let judgeEquation
    let num = b * b - 4 * a * c
    if (num < 0) {
      judgeEquation = '无实根'
    } else {
      judgeEquation = utils.getSquare(num)
    }
    console.log('num是', num)

    console.log('判别式是', judgeEquation)

    this.setData({
      topPoint: {
        x: topPointX,
        y: topPointY
      },
      crossPointY: {
        fenzi: c
      },
      squareNum: judgeEquation,
      showRet: true,
      isTopRetMinusX: topPointX.isRetMinus,
      isTopRetMinusY: topPointY.isRetMinus
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})